var mongoose = require('mongoose');
const News = require('../models/news.model');
exports.create = function(req,res,next){
    console.log(req.body);
    const news = new News(req.body);
    news.save().then(data=>{
        res.json(data);
    })
};

exports.update = function(req,res,next){
    const id = req.params.id;
    News.findByIdAndUpdate(id,{$set:req.body},{new:false}).then(data=>{
        res.json(data);
    });
};

exports.remove = function(req,res,next){
    const id = req.params.id;
    News.findByIdAndRemove(id,(err,data)=>{
        res.json({message:"delete"});
    })
};

exports.list = function(req,res,next){
    var page = req.body.page ? req.body : 1;
    var limit = req.body.limit ? req.body.limit : 5;

    var queryCondition = {};
    if(req.body.name && req.body.name.trim().length>0){
        name=req.body.name;
        queryCondition = {
            name:new RegExp(name,"i")
        };
    };
    console.log(queryCondition)
    News.paginate(queryCondition,{page:page,limit:limit},function(err,result){
        console.log(result);
        res.json(result);
    })

};